<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}
/**
 * Class account extends MY_Controller
 * @author zhangchangshun
 * 移动端用户接口类(圈子和圈子发布、圈子详情)以及获取附近位置信息
 */
class circle extends API_Controller {

	public function __construct() {
		parent::__construct();
        ini_set('date.timezone','Asia/Shanghai'); // 'Asia/Shanghai'
        $this->load->model('circle_model','mcircle');
        $this->load->model('topic_model','mtopic');
        $this->load->model('account_model','maccount');
        $this->load->model('upload_model','mupload');
        $this->load->model('comments_model','mcomments');
        $this->load->model('larticle_topic_model','mlarticle_topic');
        $this->load->model('comment_msg_model','mcommentmsg');
        $this->load->model('collects_model', 'mcollects');
        $this->load->model('concerns_model', 'mconcerns');
        $this->load->model('praise_model', 'mpraise');
        $this->load->model('long_article_model', 'mlarticle');
        $this->load->model('needs_model','mneeds');
        $this->load->model('claim_model','mclaim');
        $this->load->model('company_model','mcompany');
        $this->load->model('category_model','mcategory');



    }

    /**
     * 我的圈子
     */
    public function circle_list(){
        if(ENVIRONMENT=='production'){
            $this->_auto();
        }
        $uid = $this->userinfo['id'];
        $no_reads = $this->mcommentmsg->get_count_all(array('uid'=>$uid,'is_read'=>0));

        $query = $this->db->select('rid')
            ->from("boc_concern_fans")
            ->where(array('uid'=>$uid,'is_deleted'=>1))
            ->get();
        $uids = $query->result_array();
        $uides = array($uid);
        if(!empty($uids)){
            foreach($uids as $val){
                $uides[] = $val['rid'];
            }
            $start = $this->input->post('page');
            $limit = 8;
            $start = $limit*($start-1);
            $filed = "id,uid,audit,flag,c_type,t_id,rid,r_content,r_content1,photo,locations,repeats,comments,title,content,timeline,click";
            $circle_result = $this->db->select($filed)
                ->from("boc_circle")
                ->where(array('in'=>array('uid',$uides),'is_deleted'=>0,'audit'=>1))
                ->order_by('timeline','desc')
                ->limit($limit,$start)
                ->get();
            $comments = $circle_result->result_array();
            if(empty($comments)){
                $this->vdata['returnCode']   = '200';
                $this->vdata['returnInfo'] = '获取成功';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= array();
            }else{
                foreach($comments as $kk=>$vv){
                    $comments[$kk]['circle_photo'] = array();
                    if($vv['uid']==$uid){
                        $comments[$kk]['is_can']=0;
                    }else{
                        $comments[$kk]['is_can']=1;
                    }
                    //是否收藏和关注
                    $my_collect = $this->mcollects->get_one(array('uid'=>$uid,'tid'=>$vv['t_id'],'is_deleted'=>1),'id');
                    if(!empty($my_collect)){
                        $comments[$kk]['is_collect']=0;
                    }else{
                        $comments[$kk]['is_collect']=1;
                    }
                    //是否收藏和关注
                    $my_concerns = $this->mconcerns->get_one(array('uid'=>$uid,'tid'=>$vv['t_id'],'is_deleted'=>1),'id');
                    if(!empty($my_concerns)){
                        $comments[$kk]['is_concern']=0;
                    }else{
                        $comments[$kk]['is_concern']=1;
                    }
                    if($vv['rid']==0){
                        //话题
                        if($vv['c_type']==3){
                            //获取话题的内容
                            $topic = $this->mtopic->get_one(array('id'=>$vv['t_id']),'id,title,content,concern,comments');
                            if(!empty($topic)){
                                if($topic['content']!=''){
                                    $string = str_replace('&nbsp;','',strip_tags(userTextDecode($topic['content'])));
                                    if(strlen($string) >= 100){
                                        $comments[$kk]['content'] = content_br(mb_substr($string,0,45,'utf-8')).'...';
                                    }else{
                                        $comments[$kk]['content'] =  content_br($string);
                                    }
                                }else{
                                    $comments[$kk]['content'] ='';
                                }
                                $comments[$kk]['title'] = $topic['title'];
                                $comments[$kk]['concern'] = $topic['concern'];
                                $comments[$kk]['comments'] = $topic['comments'];
                            }else{
                                $comments[$kk]['content'] = array('c_type'=>3,'deleted'=>1,'content'=>'原话题已删除');
                            }
                        }elseif($vv['c_type']==2){
                            //获取长文章的内容
                            $larticle = $this->mlarticle->get_one(array('id'=>$vv['t_id'],'is_deleted'=>0,'audit'=>1),'id,content,title,collects');
                            if(!empty($larticle)){
                                if($larticle['content']!=''){
                                    $string = str_replace('&nbsp;','',strip_tags(userTextDecode($larticle['content'])));
                                    if(strlen($string) >= 100){
                                        $comments[$kk]['content'] = content_br(mb_substr($string,0,45,'utf-8')).'...';
                                    }else{
                                        $comments[$kk]['content'] =  content_br($string);
                                    }
                                }else{
                                    $comments[$kk]['content'] = '';
                                }
                                $comments[$kk]['title'] = $larticle['title'];
                                $comments[$kk]['collects'] = $larticle['collects'];
                            }else{
                                $comments[$kk]['content'] = array('c_type'=>2,'deleted'=>1,'content'=>'原文章已删除');
                            }

                        }elseif($vv['c_type']==1){
                            //0426修改
                            if($vv['content']!=''){
                                $string = str_replace('&nbsp;','',strip_tags(userTextDecode($vv['content'])));
                                if(strlen($string) >= 100){
                                    $comments[$kk]['content'] = content_br(mb_substr($string,0,45,'utf-8')).'...';
                                }else{
                                    $comments[$kk]['content'] =  content_br($string);
                                }
                            }else{
                                $comments[$kk]['content'] ='';
                            }
                            $circle_photo= array();
                            if($vv['photo']!=''){
                                $c_photos = explode(',',$vv['photo']);
                                foreach($c_photos as $c_photo){
                                    $img = $this->mupload->get_one(array('id'=>$c_photo),'url');
                                    if(!empty($img)){
                                        $circle_photo[] = UPLOAD_URL.$img['url'];
                                    }
                                }
                            }
                            $comments[$kk]['circle_photo'] = $circle_photo;
                        }
                    }else{
                        if($vv['c_type']==2){
                            //获取长文章的内容
                            $larticle = $this->mlarticle->get_one(array('id'=>$vv['t_id'],'is_deleted'=>0,'audit'=>1),'id,title,content,collects');
                            if(empty($larticle)){
                                $comments[$kk]['content'] = array('c_type'=>2,'deleted'=>1,'content'=>'原文章已删除');
                            }else{
                                if($larticle['content']!=''){
                                    $string = str_replace('&nbsp;','',strip_tags(userTextDecode($larticle['content'])));
                                    if(strlen($string) >= 100){
                                        $larticle_info['content'] = content_br(mb_substr($string,0,45,'utf-8')).'...';
                                    }else{
                                        $larticle_info['content'] =  content_br($string);
                                    }
                                }else{
                                    $larticle_info['content'] ='';
                                }
                                $larticle_info['title'] = $larticle['title'];
                                $larticle_info['c_type'] = 2;
                                $larticle_info['t_id'] = $larticle['id'];
                                $comments[$kk]['content'] = $larticle_info;
                            }
                        }
                        if($vv['c_type']==1){
                            //获取原来圈子的内容
                            $circle = $this->mcircle->get_one(array('id'=>$vv['t_id'],'is_deleted'=>0,'audit'=>1),'id,c_type,t_id,rid,uid,content,photo,r_content,r_content1,locations');
                            if(empty($circle)){
                                $comments[$kk]['content'] = array('c_type'=>1,'deleted'=>1,'content'=>'原圈子已删除');
                            }else{
                                $user_info1 = $this->maccount->get_one(array('id'=>$circle['uid']),'uname,photo');
                                $circle['uname']=$user_info1['uname'];
                                $photo1 = $this->mupload->get_one(array('id'=>$user_info1['photo']),'url');
                                if(!empty($photo1)){
                                    $circle['headimg'] = UPLOAD_URL.$photo1['url'];
                                }else{
                                    $circle['headimg'] = '';
                                }
                                $circle_photo1 = array();
                                if($circle['photo']!=''){
                                    $c_photos = explode(',',$circle['photo']);
                                    foreach($c_photos as $c_photo){
                                        $img = $this->mupload->get_one(array('id'=>$c_photo));
                                        if(!empty($img)){
                                            $circle_photo1[] = UPLOAD_URL.$img['url'];
                                        }
                                    }
                                }
                                if($circle['content']!=''){
                                    $string = str_replace('&nbsp;','',strip_tags(userTextDecode($circle['content'])));
                                    if(strlen($string) >= 100){
                                        $circle['content'] = content_br(mb_substr($string,0,45,'utf-8')).'...';
                                    }else{
                                        $circle['content'] =  content_br($string);
                                    }
                                }else{
                                    $circle['content'] ='';
                                }
                                $circle['circle_photo'] = $circle_photo1;
                                $comments[$kk]['content'] = $circle;
                            }
                        }
                    }
                    $rcontents = @json_decode($comments[$kk]['r_content1']);
                    if(!empty($rcontents)){
                        foreach($rcontents as $kkl=>$vvl){
                            if($vvl->r_content!=''){
                                $stringll = str_replace('&nbsp;','',strip_tags(userTextDecode($vvl->r_content)));
                                if(strlen($stringll) >= 100){
                                    $vvl->r_content = mb_substr($stringll,0,45,'utf-8').'...';
                                }else{
                                    $vvl->r_content = $stringll;
                                }
                            }
                        }
                    }

                    $comments[$kk]['r_content1'] = $rcontents;
                    if($comments[$kk]['r_content']!= ''){
                        $stringll2 = str_replace('&nbsp;','',strip_tags(userTextDecode($comments[$kk]['r_content'])));
                        if(strlen($stringll2) >= 100){
                            $comments[$kk]['r_content'] = mb_substr($stringll2,0,45,'utf-8').'...';
                        }else{
                            $comments[$kk]['r_content'] = $stringll2;
                        }
                    }
                    $user_info = $this->maccount->get_one(array('id'=>$vv['uid']),'uname,photo');
                    $comments[$kk]['uname']=$user_info['uname'];
                    $photo = $this->mupload->get_one(array('id'=>$user_info['photo']),'url');
                    if(!empty($photo)){
                        $comments[$kk]['headimg'] = UPLOAD_URL.$photo['url'];
                    }else{
                        $comments[$kk]['headimg'] = '';
                    }

                    $now_time = time();
                    if($vv['timeline']+24*3600 < $now_time){
                        $comments[$kk]['cre_time'] = date('m-d H:i',$vv['timeline']);
                    }else{
                        if($vv['timeline']+3600 < $now_time){
                            $surpose_time = $now_time-$vv['timeline'];
                            $res = round($surpose_time/3600);
                            $comments[$kk]['cre_time'] = $res.'小时前';
                        }elseif($vv['timeline']+60 < $now_time){
                            $surpose_time = $now_time-$vv['timeline'];
                            $res = round($surpose_time/60);
                            $comments[$kk]['cre_time'] = $res.'分钟前';
                        }elseif($now_time - $vv['timeline'] < 60){
                            $comments[$kk]['cre_time'] = '1分钟内';
                        }
                    }
                }
                $info['comments'] = $comments;
                $info['no_reads'] = $no_reads;
                $this->vdata['returnCode']   = '200';
                $this->vdata['returnInfo'] = '获取成功';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= $info;
            }
        }else{
            $this->vdata['returnCode']   = '400';
            $this->vdata['returnInfo'] = '去关注';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }

        $this->_send_json($this->vdata);
    }

    /**
     * 热门
     */
    public function hot_list(){
        $uid = $this->userinfo['id'];
        $keyword = $this->input->post('keyword');
        $start = $this->input->post('page');
        $limit = 5;
        $start = $limit*($start-1);
        $filed = "*";
        $where = array('like'=>array('title',$keyword));
        $hot_result = $this->db->select($filed)
            ->from("boc_larticle_topic")
            ->where($where)
            ->order_by('flag desc,hots desc,timeline desc')
            ->limit($limit,$start)
            ->get();
        $comments = $hot_result->result_array();
        if(empty($comments)){
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '获取成功';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else{
            foreach($comments as $kk=>$vv){
                $topic_photo = array();
                if($vv['photo']!=''){
                    $photo_result = explode(',',$vv['photo']);
                    foreach($photo_result as $c_photo){
                        $img = $this->mupload->get_one(array('id'=>$c_photo),'url');
                        if(!empty($img)){
                            $topic_photo[] = UPLOAD_URL.$img['url'];
                        }
                    }
                }

                $comments[$kk]['topic_photos'] = $topic_photo;
                //是否收藏和关注
                $my_collect = $this->mcollects->get_one(array('uid'=>$uid,'tid'=>$vv['id'],'is_deleted'=>1),'id');
                if(!empty($my_collect)){
                    $comments[$kk]['is_collect']=0;
                }else{
                    $comments[$kk]['is_collect']=1;
                }
                //是否收藏和关注
                $my_concerns = $this->mconcerns->get_one(array('uid'=>$uid,'tid'=>$vv['id'],'is_deleted'=>1),'id');
                if(!empty($my_concerns)){
                    $comments[$kk]['is_concern']=0;
                }else{
                    $comments[$kk]['is_concern']=1;
                }
                //是否点赞
                $my_praise = $this->mpraise->get_one(array('uid'=>$uid,'tid'=>$vv['id'],'is_deleted'=>1),'id');
                if(!empty($my_praise)){
                    $comments[$kk]['is_praise']=0;
                }else{
                    $comments[$kk]['is_praise']=1;
                }
                if($vv['content']!=''){
                    $string = str_replace('&nbsp;','',strip_tags(userTextDecode($vv['content'])));
                    if(strlen($string) >= 100){
                        $comments[$kk]['content'] = content_br(mb_substr($string,0,45,'utf-8')).'...';
                    }else{
                        $comments[$kk]['content'] =  content_br($string);
                    }
                }else{
                    $comments[$kk]['content'] = '';
                }
                if($vv['cate']==1){
                    $comments[$kk]['c_type']=2;
                    //获取发布长文章时生成的圈子的信息
                    $comments[$kk]['circle_info'] = $this->mcircle->get_one(array('t_id'=>$vv['id'],'c_type'=>2,'rid'=>0,'audit'=>1));
                }elseif($vv['cate']==2){
                    $comments[$kk]['c_type']=3;
                }

                $now_time = time();
                if($vv['timeline']+24*3600 < $now_time){
                    $comments[$kk]['cre_time'] = date('m-d H:i',$vv['timeline']);
                }else{
                    if($vv['timeline']+3600 < $now_time){
                        $surpose_time = $now_time-$vv['timeline'];
                        $res = round($surpose_time/3600);
                        $comments[$kk]['cre_time'] = $res.'小时前';
                    }elseif($vv['timeline']+60 < $now_time){
                        $surpose_time = $now_time-$vv['timeline'];
                        $res = round($surpose_time/60);
                        $comments[$kk]['cre_time'] = $res.'分钟前';
                    }elseif($now_time - $vv['timeline'] < 60){
                        $comments[$kk]['cre_time'] = '1分钟内';
                    }
                }
            }
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '获取成功';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= $comments;
        }
        $this->_send_json($this->vdata);

    }

    /**
     * 内容管理
     */
    public function my_contents(){
        $ctype = $this->input->post("ctype");
        if($ctype==0){
            $query = $this->db->select('need_id')
                ->from("boc_claim")
                ->where(array('uid'=>$this->userinfo['id'],'is_deleted'=>0))
                ->get();
            $need_id = $query->result_array();
            $need_ids = array();
            $uid = $this->userinfo['id'];
            if(!empty($need_id)){
                foreach ($need_id as $val) {
                    $need_ids[] = $val['need_id'];
                }
                $need_idss = implode(',',$need_ids);
                $start = $this->input->post('page');
                $limit = 10;
                $start = $limit*($start-1);
                $results = $this->db->query("select * from boc_need_circle as a  where a.uid=$uid UNION select * from boc_need_circle as b where b.nc_id in ($need_idss) and b.cate=2 order by cre_time desc limit $start,$limit")->result_array();
            }else{
                $start = $this->input->post('page');
                $limit = 10;
                $start = $limit*($start-1);
                $results = $this->db->query("select * from boc_need_circle as a  where a.uid=$uid order by cre_time desc limit $start,$limit")->result_array();
            }
        }elseif($ctype==1){
            //我的需求（我发布的和我认领的）
            //全部
            $query = $this->db->select('need_id')
                ->from("boc_claim")
                ->where(array('uid'=>$this->userinfo['id'],'is_deleted'=>0))
                ->get();
            $need_id = $query->result_array();
            $need_ids = array();
            if(!empty($need_id)){
                foreach ($need_id as $val) {
                    $need_ids[] = $val['need_id'];
                }
                $start = $this->input->post('page');
                $limit = 10;
                $start = $limit*($start-1);
                $filed = "*,created as cre_time,intro as contents,photos as pics";
                $needs_result = $this->db->select($filed)
                    ->from("boc_needs")
                    ->where(array('in'=>array('id',$need_ids),'is_deleted'=>0))
                    ->or_where(array('uid'=>$this->userinfo['id']))
                    ->order_by('created','desc')
                    ->limit($limit,$start)
                    ->get();
                $results = $needs_result->result_array();
            }else{
                $start = $this->input->post('page');
                $limit = 10;
                $start = $limit*($start-1);
                $filed = "*,created as cre_time,intro as contents,photos as pics";
                $needs_result = $this->db->select($filed)
                    ->from("boc_needs")
                    ->where(array('uid'=>$this->userinfo['id'],'is_deleted'=>0))
                    ->order_by('created','desc')
                    ->limit($limit,$start)
                    ->get();
                $results = $needs_result->result_array();
            }
        }elseif($ctype==2){
            //我的圈子 我发布的和我转发的
            $start = $this->input->post('page');
            $limit = 10;
            $start = $limit*($start-1);
            $filed = "id,uid,audit,flag,c_type,t_id,rid,r_content,r_content1,photo as pics,locations,repeats,comments,title,content as contents,timeline as cre_time,click";
            $circle_result = $this->db->select($filed)
                ->from("boc_circle")
                ->where(array('uid'=>$this->userinfo['id'],'is_deleted'=>0,'audit'=>1))
                ->order_by('timeline','desc')
                ->limit($limit,$start)
                ->get();
            $results = $circle_result->result_array();
        }
        if(empty($results)){
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '获取成功';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else{
            foreach($results as $kk=>$vv){
                $results[$kk]['month'] = date('m',$vv['cre_time']);
                $results[$kk]['day'] = date('d',$vv['cre_time']);
                if($vv['title']!=''){
                    $results[$kk]['contents'] = $vv['title'];
                }else{
                    if($vv['contents']!=''){
                        $string = str_replace('&nbsp;','',strip_tags(userTextDecode($vv['contents'])));
                        if(strlen($string) >= 100){
                            $results[$kk]['contents'] = content_br(mb_substr($string,0,45,'utf-8')).'...';
                        }else{
                            $results[$kk]['contents'] = content_br($string);
                        }
                    }else{
                        if($vv['r_content']!=''){
                            $results[$kk]['contents'] = $vv['r_content'];
                        }else{
                            $results[$kk]['contents'] = '';
                        }
                    }
                }
                if($vv['pics']!=''){
                    $photo_result = explode(',',$vv['pics']);
                    $results[$kk]['pics_nums'] = count($photo_result);
                    $show_photo = $photo_result[0];
                    if(isset($show_photo) && $show_photo!=''){
                        $photo = $this->mupload->get_one(array('id'=>$show_photo),'url');
                        if(!empty($photo)){
                            $results[$kk]['pics'] = UPLOAD_URL.$photo['url'];
                        }else{
                            $results[$kk]['pics'] = '';
                        }
                    }else{
                        $results[$kk]['pics'] = '';
                    }
                }else{
                    $results[$kk]['pics'] = '';
                    $results[$kk]['pics_nums'] = 0;
                }
                //判断是不是自己的
                if($vv['uid']==$this->userinfo['id']){
                    $results[$kk]['is_me'] = 1;
                }else{
                    $results[$kk]['is_me'] = 0;
                }
                if($ctype==0){
                    if($vv['cate']==1 && $vv['rid']!=0){
                        $circle_info = $this->mcircle->get_one(array('id'=>$vv['nc_id']),'r_content,t_id,c_type');
                        $info['title'] = $circle_info['r_content'];
                        $info['content'] = '';
                        if($circle_info['c_type']==1){
                            $circle_info_in = $this->mcircle->get_one(array('id'=>$circle_info['t_id'],'is_deleted'=>0,'audit'=>1),'content,id');
                            if(empty($circle_info_in)){
                                $info = array('deleted'=>1,'content'=>'原圈子已删除','c_type'=>1);
                            }else{
                                if($circle_info_in['content']!=''){
                                    $string = str_replace('&nbsp;','',strip_tags(userTextDecode($circle_info_in['content'])));
                                    if(strlen($string) >= 100){
                                        $info['content'] = content_br(mb_substr($string,0,45,'utf-8')).'...';
                                    }else{
                                        $info['content'] = content_br($string);
                                    }
                                }else{
                                    $info['content'] = '';
                                }
                                $info['c_type'] = 1;
                                $info['tid'] = $circle_info_in['id'];
                            }
                        }elseif($circle_info['c_type']==2){
                            $circle_info_in = $this->mlarticle->get_one(array('id'=>$circle_info['t_id'],'is_deleted'=>0,'audit'=>1),'title,id');
                            if(empty($circle_info_in)){
                                $info = array('deleted'=>1,'content'=>'原文章已删除','c_type'=>2);
                            }else{
//                                $info['content'] = mb_substr(str_replace('&nbsp;','',strip_tags(userTextDecode($circle_info_in['content']))),0,45,'utf-8').'...';
                                $info['content'] = $circle_info_in['title'];
                                $info['c_type'] = 2;
                                $info['tid'] = $circle_info_in['id'];
                            }
                        }elseif($circle_info['c_type']==3){
                            $circle_info_in = $this->mtopic->get_one(array('id'=>$circle_info['t_id'],'is_deleted'=>0,'audit'=>1),'title,id');
                            if(empty($circle_info_in)){
                                $info = array('deleted'=>1,'content'=>'原话题已删除','c_type'=>3);
                            }else{
//                                $info['content'] = mb_substr(str_replace('&nbsp;','',strip_tags(userTextDecode($circle_info_in['content']))),0,45,'utf-8').'...';
                                $info['content'] = $circle_info_in['title'];
                                $info['c_type'] = 3;
                                $info['tid'] = $circle_info_in['id'];
                            }
                        }
                        $results[$kk]['content'] = $info;
                    }elseif($vv['cate']==2){
                        //认领情况
                        $claim_info = $this->mclaim->get_one(array('uid'=>$this->userinfo['id'],'need_id'=>$vv['nc_id']),'is_assign');
                        if(empty($claim_info)){
                            $results[$kk]['is_claim'] = 0;
                        }else{
                            $results[$kk]['is_claim'] = 1;
                            if($claim_info['is_assign']==1){
                                $results[$kk]['is_assign'] = 1;         //已指派
                            }else{
                                $results[$kk]['is_assign'] = 0;         //未指派
                            }
                        }
                    }
                }elseif($ctype==2){
                    if($vv['rid']!=0){
                        $circle_info = $this->mcircle->get_one(array('id'=>$vv['id']),'r_content,t_id,c_type');
                        $info['title'] = $circle_info['r_content'];
                        $info['content'] = '';
                        if($circle_info['c_type']==1){
                            $circle_info_in = $this->mcircle->get_one(array('id'=>$circle_info['t_id'],'is_deleted'=>0,'audit'=>1),'content,id');
                            if(empty($circle_info_in)){
                                $info = array('deleted'=>1,'content'=>'原圈子已删除','c_type'=>1);
                            }else{
                                if($circle_info_in['content']!=''){
                                    $string = str_replace('&nbsp;','',strip_tags(userTextDecode($circle_info_in['content'])));
                                    if(strlen($string) >= 100){
                                        $info['content'] = content_br(mb_substr($string,0,45,'utf-8')).'...';
                                    }else{
                                        $info['content'] =  content_br($string);
                                    }
                                }else{
                                    $info['content'] = '';
                                }
                                $info['c_type'] = 1;
                                $info['tid'] = $circle_info_in['id'];
                            }
                        }elseif($circle_info['c_type']==2){
                            $circle_info_in = $this->mlarticle->get_one(array('id'=>$circle_info['t_id'],'is_deleted'=>0,'audit'=>1),'title,id');
                            if(empty($circle_info_in)){
                                $info = array('deleted'=>1,'content'=>'原文章已删除','c_type'=>2);
                            }else{
//                                $info['content'] = mb_substr(str_replace('&nbsp;','',strip_tags(userTextDecode($circle_info_in['content']))),0,45,'utf-8').'...';
                                $info['content'] = $circle_info_in['title'];
                                $info['c_type'] = 2;
                                $info['tid'] = $circle_info_in['id'];
                            }
                        }elseif($circle_info['c_type']==3){
                            $circle_info_in = $this->mtopic->get_one(array('id'=>$circle_info['t_id'],'is_deleted'=>0,'audit'=>1),'title,id');
                            if(empty($circle_info_in)){
                                $info = array('deleted'=>1,'content'=>'原话题已删除','c_type'=>3);
                            }else{
//                                $info['content'] = mb_substr(str_replace('&nbsp;','',strip_tags(userTextDecode($circle_info_in['content']))),0,45,'utf-8').'...';
                                $info['content'] = $circle_info_in['title'];
                                $info['c_type'] = 3;
                                $info['tid'] = $circle_info_in['id'];
                            }
                        }
                        $results[$kk]['content'] = $info;
                    }
                }elseif($ctype==1){
                    //认领情况
                    $claim_info = $this->mclaim->get_one(array('uid'=>$this->userinfo['id'],'need_id'=>$vv['id']),'is_assign');
                    if(empty($claim_info)){
                        $results[$kk]['is_claim'] = 0;
                    }else{
                        $results[$kk]['is_claim'] = 1;
                        if($claim_info['is_assign']==1){
                            $results[$kk]['is_assign'] = 1;         //已指派
                        }else{
                            $results[$kk]['is_assign'] = 0;         //未指派
                        }
                    }
                }
            }
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '获取成功';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= $results;
        }
        $this->_send_json($this->vdata);
    }

    /***
     * 企业动态接口
     */
    public function company_state()
    {
        $type = $this->input->post('type');
        $company_id= $this->input->post('cid');
        if($company_id == 0) {
            $this->vdata['returnCode'] = '250';
            $this->vdata['returnInfo'] = '请求失败';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = array();
        }else{
            $company_info = $this->mcompany->get_one(array('id'=>$company_id),'id,name');
            $company_name = $company_info['name'];
            $query = $this->db->select('id')
                ->from("boc_account")
                ->where(array('company' => $company_id))
                ->get();
            $querys = $query->result_array();
            $uids = array();
            foreach ($querys as $val) {
                $uids[] = $val['id'];
            }
            if ($type == 1) {
                //所有圈子
                //企业所有人发布的圈子+非企业所有人发布的圈子中标题含企业简称的+内容中简称存在至少为三次的
                //内容中简称至少为三次存在的
                $company_info = $this->mcompany->get_one(array('id' => $company_id));
                $title = $company_info['short'];
                $title_count = $this->db->query("select id from boc_circle where (length(content)-length(replace(content,'$title','')))/length('$title') >= 3 and title not like '%$title%' and is_deleted=0")->result_array();
                $title_cot = $this->db->query("select id from boc_circle where (length(content)-length(replace(content,'$title','')))/length('$title') < 3 and title like '%$title%' and is_deleted=0")->result_array();
                $where = array('in' => array('uid', $uids), 'is_deleted' => 0);
                $where11 = array('or_not_in' => array('uid', $uids), 'is_deleted' => 0);
                $where22 = array('or_not_in' => array('uid', $uids), 'is_deleted' => 0);
                $title_id1 = array();
                $title_id2 = array();
                if (!empty($title_count)) {
                    foreach ($title_count as $vt) {
                        $title_id1[] = $vt['id'];
                    }
                    $where1 = array('in' => array('id', $title_id1));
                    $where11 = array_merge($where11, $where1);
                }
                if (!empty($title_cot)) {
                    foreach ($title_cot as $vtt) {
                        $title_id2[] = $vtt['id'];
                    }
                    $where2 = array('in' => array('id', $title_id2));
                    $where22 = array_merge($where22, $where2);
                }
                $start = $this->input->post('page');
                $limit = 8;
                $start = $limit * ($start - 1);
                $filed = "id,uid,audit,flag,c_type,t_id,rid,r_content,r_content1,photo,locations,repeats,comments,title,content,timeline,click,is_deleted";
                if(!empty($title_count) && !empty($title_cot)){
                    $circle_result = $this->db->select($filed)
                        ->from("boc_circle")
                        ->where($where)
                        ->where($where11)
                        ->where($where22)
                        ->order_by('timeline', 'desc')
                        ->limit($limit, $start)
                        ->get();
                }elseif(!empty($title_count) && empty($title_cot)){
                    $circle_result = $this->db->select($filed)
                        ->from("boc_circle")
                        ->where($where)
                        ->where($where11)
                        ->order_by('timeline', 'desc')
                        ->limit($limit, $start)
                        ->get();
                }elseif(empty($title_count) && !empty($title_cot)){
                    $circle_result = $this->db->select($filed)
                        ->from("boc_circle")
                        ->where($where)
                        ->where($where22)
                        ->order_by('timeline', 'desc')
                        ->limit($limit, $start)
                        ->get();
                }elseif(empty($title_count) && empty($title_cot)){
                    $circle_result = $this->db->select($filed)
                        ->from("boc_circle")
                        ->where($where)
                        ->order_by('timeline', 'desc')
                        ->limit($limit, $start)
                        ->get();
                }
                $comments = $circle_result->result_array();
                if(empty($comments)){
                    $this->vdata['returnCode']   = '200';
                    $this->vdata['returnInfo'] = '获取成功';
                    $this->vdata['secure']     = JSON_SECURE;
                    $this->vdata['content']= array();
                }else{
                    foreach($comments as $kk=>$vv){
                        $comments[$kk]['circle_photo'] = array();
                        $uid = $this->userinfo['id'];
                        if($vv['uid']==$uid){
                            $comments[$kk]['is_can']=0;
                        }else{
                            $comments[$kk]['is_can']=1;
                        }
                        //是否收藏和关注
                        $my_collect = $this->mcollects->get_one(array('uid'=>$uid,'tid'=>$vv['t_id'],'is_deleted'=>1),'id');
                        if(!empty($my_collect)){
                            $comments[$kk]['is_collect']=0;
                        }else{
                            $comments[$kk]['is_collect']=1;
                        }
                        //是否收藏和关注
                        $my_concerns = $this->mconcerns->get_one(array('uid'=>$uid,'tid'=>$vv['t_id'],'is_deleted'=>1),'id');
                        if(!empty($my_concerns)){
                            $comments[$kk]['is_concern']=0;
                        }else{
                            $comments[$kk]['is_concern']=1;
                        }
                        if($vv['rid']==0){
                            //话题
                            if($vv['c_type']==3){
                                //获取话题的内容
                                $topic = $this->mtopic->get_one(array('id'=>$vv['t_id']),'content,title,concern');
                                $comments[$kk]['content'] = $topic['content'];
                                $comments[$kk]['title'] = $topic['title'];
                                $comments[$kk]['concern'] = $topic['concern'];
                            }elseif($vv['c_type']==2){
                                //获取长文章的内容
                                $larticle = $this->mlarticle->get_one(array('id'=>$vv['t_id'],'is_deleted'=>0,'audit'=>1),'id,content,title,collects');
                                if(!empty($larticle)){
                                    if($larticle['content']!=''){
                                        $string = str_replace('&nbsp;','',strip_tags(userTextDecode($larticle['content'])));
                                        if(strlen($string) >= 100){
                                            $comments[$kk]['content'] = content_br(mb_substr($string,0,45,'utf-8')).'...';
                                        }else{
                                            $comments[$kk]['content'] = content_br($string);
                                        }
                                    }else{
                                        $comments[$kk]['content'] ='';
                                    }
                                    $comments[$kk]['title'] = $larticle['title'];
                                    $comments[$kk]['collects'] = $larticle['collects'];
                                }else{
                                    $comments[$kk]['content'] = array('c_type'=>2,'deleted'=>1,'content'=>'原文章已删除');
                                }

                            }elseif($vv['c_type']==1){
                                //0426修改
                                if($vv['content']!=''){
                                    $string = str_replace('&nbsp;','',strip_tags(userTextDecode($vv['content'])));
                                    if(strlen($string) >= 100){
                                        $comments[$kk]['content'] = content_br(mb_substr($string,0,45,'utf-8')).'...';
                                    }else{
                                        $comments[$kk]['content'] = content_br($string);
                                    }
                                }else{
                                    $comments[$kk]['content'] = '';
                                }
                                $circle_photo= array();
                                if($vv['photo']!=''){
                                    $c_photos = explode(',',$vv['photo']);
                                    foreach($c_photos as $c_photo){
                                        $img = $this->mupload->get_one(array('id'=>$c_photo),'url');
                                        if(!empty($img)){
                                            $circle_photo[] = UPLOAD_URL.$img['url'];
                                        }
                                    }
                                }
                                $comments[$kk]['circle_photo'] = $circle_photo;
                            }
                        }else{
                            if($vv['c_type']==2){
                                //获取长文章的内容
                                $larticle = $this->mlarticle->get_one(array('id'=>$vv['t_id'],'is_deleted'=>0,'audit'=>1),'id,content,title');
                                if(empty($larticle)){
                                    $comments[$kk]['content'] = array('c_type'=>2,'deleted'=>1,'content'=>'原文章已删除');
                                }else{
                                    if($larticle['content']!=''){
                                        $string = str_replace('&nbsp;','',strip_tags(userTextDecode($larticle['content'])));
                                        if(strlen($string) >= 100){
                                            $larticle_info['content'] = content_br(mb_substr($string,0,45,'utf-8')).'...';
                                        }else{
                                            $larticle_info['content'] = content_br($string);
                                        }
                                    }else{
                                        $larticle_info['content'] = '';
                                    }
                                    $larticle_info['title'] = $larticle['title'];
                                    $larticle_info['c_type'] = 2;
                                    $larticle_info['t_id'] = $larticle['id'];
                                    $comments[$kk]['content'] = $larticle_info;
                                }
                            }
                            if($vv['c_type']==1){
                                //获取原来圈子的内容
                                $circle = $this->mcircle->get_one(array('id'=>$vv['t_id'],'is_deleted'=>0,'audit'=>1),'id,c_type,t_id,rid,r_content,r_content1,content,uid,photo');
                                if(empty($circle)){
                                    $comments[$kk]['content'] = array('deleted'=>1,'content'=>'原圈子已删除','c_type'=>1);
                                }else{
                                    $user_info1 = $this->maccount->get_one(array('id'=>$circle['uid']),'uname,photo');
                                    $circle['uname']=$user_info1['uname'];
                                    $photo1 = $this->mupload->get_one(array('id'=>$user_info1['photo']),'url');
                                    if(!empty($photo1)){
                                        $circle['headimg'] = UPLOAD_URL.$photo1['url'];
                                    }else{
                                        $circle['headimg'] = '';
                                    }
                                    $circle_photo1 = array();
                                    if($circle['photo']!=''){
                                        $c_photos = explode(',',$circle['photo']);
                                        foreach($c_photos as $c_photo){
                                            $img = $this->mupload->get_one(array('id'=>$c_photo),'url');
                                            if(!empty($img)){
                                                $circle_photo1[] = UPLOAD_URL.$img['url'];
                                            }
                                        }
                                    }
                                    if($circle['content']!=''){
                                        $string = str_replace('&nbsp;','',strip_tags(userTextDecode($circle['content'])));
                                        if(strlen($string) >= 100){
                                            $circle['content'] = content_br(mb_substr($string,0,45,'utf-8')).'...';
                                        }else{
                                            $circle['content'] =  content_br($string);
                                        }
                                    }else{
                                        $circle['content'] ='';
                                    }
                                    $circle['circle_photo'] = $circle_photo1;
                                    $comments[$kk]['content'] = $circle;
                                    $comments[$kk]['circle_photo'] = array();
                                }
                            }
                        }
                        $comments[$kk]['r_content1'] = @json_decode($comments[$kk]['r_content1']);
                        $user_info = $this->maccount->get_one(array('id'=>$vv['uid']),'uname,photo');
                        $comments[$kk]['uname']=$user_info['uname'];
                        $photo = $this->mupload->get_one(array('id'=>$user_info['photo']),'url');
                        if(!empty($photo)){
                            $comments[$kk]['headimg'] = UPLOAD_URL.$photo['url'];
                        }else{
                            $comments[$kk]['headimg'] = '';
                        }

                        $now_time = time();
                        if($vv['timeline']+24*3600 < $now_time){
                            $comments[$kk]['cre_time'] = date('m-d H:i',$vv['timeline']);
                        }else{
                            if($vv['timeline']+3600 < $now_time){
                                $surpose_time = $now_time-$vv['timeline'];
                                $res = round($surpose_time/3600);
                                $comments[$kk]['cre_time'] = $res.'小时前';
                            }elseif($vv['timeline']+60 < $now_time){
                                $surpose_time = $now_time-$vv['timeline'];
                                $res = round($surpose_time/60);
                                $comments[$kk]['cre_time'] = $res.'分钟前';
                            }elseif($now_time - $vv['timeline'] < 60){
                                $comments[$kk]['cre_time'] = '1分钟内';
                            }
                        }
                    }
                    $info['comments'] = $comments;
                    $info['company_name'] = $company_name;
                    $this->vdata['returnCode']   = '200';
                    $this->vdata['returnInfo'] = '获取成功';
                    $this->vdata['secure']     = JSON_SECURE;
                    $this->vdata['content']= $info;
                }
            }elseif($type==2){
                //发布的需求
                $start = $this->input->post('page');
                $limit = 10;
                $start = $limit*($start-1);
                $filed = "id,uid,min_money,max_money,order_sn,cate_id,type,created,intro,end_time,photos,method,status,margin,claim_money,countdown,expire_time,pay_time,is_delay,is_again,views,claim_nums,claim_status";
                $needs_result = $this->db->select($filed)
                    ->from("boc_needs")
                    ->where(array('in'=>array('uid',$uids),'is_deleted'=>0,'claim_status'=>1,'status'=>1))
                    ->order_by('created','desc')
                    ->limit($limit,$start)
                    ->get();
                $needs = $needs_result->result_array();
                if(empty($needs)){
                    $this->vdata['returnCode']   = '200';
                    $this->vdata['returnInfo'] = '获取成功';
                    $this->vdata['secure']     = JSON_SECURE;
                    $this->vdata['content']= array();
                }else{
                    foreach($needs as $key=>$val){
                        $cate = $this->mcategory->get_one(array('id'=>$val['cate_id']),'title');
                        $needs[$key]['category'] = $cate['title'];
                        $needs[$key]['intro'] = content_br($val['intro']);
                        $needs[$key]['cre_time'] = date('m-d H:i',$val['created']);
                        $time = time();
                        $needs[$key]['now_time'] = $time;
                    }
                    $this->vdata['returnCode']   = '200';
                    $this->vdata['returnInfo'] = '获取成功';
                    $this->vdata['secure']     = JSON_SECURE;
                    $this->vdata['content']= $needs;
                }
            }
        }
        $this->_send_json($this->vdata);
    }

    /**
     * 获取附近位置信息及位置搜索
     */
    public function getlocation(){
        if(ENVIRONMENT=='production'){
            $this->_auto();
        }
        $key = 'f6703866d0a7ecf3f883131d5f6356d6';    //客户的高德地图KEY
        $limit = 10;
        $location = $this->input->post('location');
        $page = $this->input->post('page');
        $keyword = $this->input->post('keyword');
        $url = "http://restapi.amap.com/v3/place/around?key=$key&location=$location&keywords=$keyword&radius=10000&types=&offset=$limit&page=$page&extensions=all";
        $result = file_get_contents($url);
        $result=json_decode($result,true);
        if($result && isset($result['pois'])){
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '操作成功';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= $result['pois'];
        }else{
            $this->vdata['returnCode']   = '250';
            $this->vdata['returnInfo'] = '已经到底了';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }
        $this->_send_json($this->vdata);
    }

    /**
     * 发布圈子
     */
    public function create_circle(){
        if(ENVIRONMENT=='production'){
            $this->_auto();
        }
        //获取当前用户信息
        $user_info = $this->userinfo;
        if(!isset($user_info) && empty($user_info)){
            $this->vdata['returnCode']   = '401';
            $this->vdata['returnInfo'] = '请求失败';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else{
            $uid = $user_info['id'];
            $data = $this->input->post();
            if(!isset($data) && empty($data)){
                $this->vdata['returnCode']   = '250';
                $this->vdata['returnInfo'] = '缺少参数';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= array();
            }else{
//                $content = $this->input->post('content');
//                if(!isset($content) || trim($content)==''){
//                    $this->vdata['returnCode']   = '250';
//                    $this->vdata['returnInfo'] = '请填写圈子内容';
//                    $this->vdata['secure']     = JSON_SECURE;
//                    $this->vdata['content']= array();
//                }
                $photos = isset($data['photos']) ? $data['photos'] : '';
                $locations = isset($data['locations']) ? $data['locations'] : '';
                $create_data = array(
                    'uid'=>$uid,
                    'content'=>isset($data['content']) ? userTextEncode(strip_tags(trim($data['content']))) : '',
                    'photo'=>$photos,
                    'locations'=>$locations,
                    'timeline'=>time()
                );
                $circle_id = $this->mcircle->create($create_data);
                if($circle_id){
                    $this->vdata['returnCode']   = '200';
                    $this->vdata['returnInfo'] = '发布成功';
                    $this->vdata['secure']     = JSON_SECURE;
                    $this->vdata['content']= $circle_id;
                }else{
                    $this->vdata['returnCode']   = '250';
                    $this->vdata['returnInfo'] = '发布失败';
                    $this->vdata['secure']     = JSON_SECURE;
                    $this->vdata['content']= array();
                }
            }
        }
        $this->_send_json($this->vdata);
    }

    /**
     * 获取原内容
     */
    public function get_sources(){
        $type = $this->input->post('ctype');
        $tid = $this->input->post('id');
        if($type==1){
            //原圈子内容
            $cirle_info = $this->mcircle->get_one(array('id'=>$tid,'is_deleted'=>0,'audit'=>1));
            if(empty($cirle_info)){
                $this->vdata['returnCode']   = '200';
                $this->vdata['returnInfo'] = '圈子不存在';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= array('content'=>'原圈子已删除','deleted'=>1,'c_type'=>1);
            }else{
                if($cirle_info['rid']==0){
                    $info = $cirle_info;
                }else{
                    if($cirle_info['c_type']==1){
                        $info = $this->mcircle->get_one(array('id'=>$cirle_info['t_id'],'is_deleted'=>0,'audit'=>1));
                    }elseif($cirle_info['c_type']==2){
                        $info = $this->mlarticle->get_one(array('id'=>$cirle_info['t_id'],'is_deleted'=>0,'audit'=>1));
                    }
                }
                if(!empty($info)){
                    if($info['photo']!=''){
                        $c_photos = explode(',',$info['photo']);
                        $c_photo = $c_photos[0];
                        $img = $this->mupload->get_one(array('id'=>$c_photo));
                        if(!empty($img)){
                            $info['circle_photo'] = UPLOAD_URL.$img['url'];
                        }else{
                            $info['circle_photo'] = '';
                        }
                    }else{
                        $info['circle_photo'] = '';
                    }
                    $info['content'] = mb_substr(str_replace('&nbsp;','',strip_tags(userTextDecode($info['content']))),0,45,'utf-8').'...';
                    $user_info = $this->maccount->get_one(array('id'=>$info['uid']));
                    if(!empty($user_info)){
                        $info['userinfo'] = $user_info;
                    }else{
                        $info['userinfo'] = array();
                    }
                    $this->vdata['returnCode']   = '200';
                    $this->vdata['returnInfo'] = '原圈子';
                    $this->vdata['secure']     = JSON_SECURE;
                    $this->vdata['content']= $info;
                }else{
                    $this->vdata['returnCode']   = '200';
                    $this->vdata['returnInfo'] = '原圈子已删除';
                    $this->vdata['secure']     = JSON_SECURE;
                    $this->vdata['content']= array('content'=>'原圈子已删除','deleted'=>1,'c_type'=>1);
                }

            }
        }elseif($type==2){
            //原长文章内容
            $cirle_info = $this->mcircle->get_one(array('id'=>$tid,'is_deleted'=>0,'audit'=>1));
            $lid = $cirle_info['t_id'];
            $info = $this->mlarticle->get_one(array('id'=>$lid,'is_deleted'=>0,'audit'=>1));
            if(!empty($info)){
                if($info['photo']!=''){
                    $c_photos = explode(',',$info['photo']);
                    $c_photo = $c_photos[0];
                    $img = $this->mupload->get_one(array('id'=>$c_photo));
                    if(!empty($img)){
                        $info['circle_photo'] = UPLOAD_URL.$img['url'];
                    }else{
                        $info['circle_photo'] = '';
                    }
                }else{
                    $info['circle_photo'] = '';
                }
                if($info['content']!=''){
                    $string = str_replace('&nbsp;','',strip_tags(userTextDecode($info['content'])));
                    if(strlen($string) >= 100){
                        $info['content'] = mb_substr($string,0,45,'utf-8').'...';
                    }else{
                        $info['content'] = $string;
                    }
                }else{
                    $info['content'] = '';
                }

                $user_info = $this->maccount->get_one(array('id'=>$info['uid']));
                if(!empty($user_info)){
                    $info['userinfo'] = $user_info;
                }else{
                    $info['userinfo'] = array();
                }
                $this->vdata['returnCode']   = '200';
                $this->vdata['returnInfo'] = '原文章';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= $info;
            }else{
                $this->vdata['returnCode']   = '200';
                $this->vdata['returnInfo'] = '原文章已删除';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= array('content'=>'原文章已删除','deleted'=>1,'c_type'=>2);
            }
        }
        $this->_send_json($this->vdata);
    }

    /**
     * 转发时获取上一个转发者名字和分享心得
     */
    public function get_repeater(){
        $rid = $this->input->post('rid');
        $cirle_info = $this->mcircle->get_one(array('id'=>$rid,'is_deleted'=>0,'audit'=>1));
        if(empty($cirle_info)){
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '圈子不存在';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else{
            if($cirle_info['rid']==0){
                $info = array();
                $this->vdata['returnCode']   = '200';
                $this->vdata['returnInfo'] = '原圈子';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= $info;
            }else{
                //获取转发者名字和分享心得
                $user_info = $this->maccount->get_one(array('id'=>$cirle_info['uid']));
                $info1 = array(array(
                    'uid'=>$cirle_info['uid'],
                    'uname'=>$user_info['uname'],
                    'r_content'=>userTextDecode($cirle_info['r_content'])
                ));
                $cir_info = @json_decode($cirle_info['r_content1']);
                if(!empty($cir_info)){
                    $info = array_merge($info1,$cir_info);
                }else{
                    $info = $info1;
                }
                $this->vdata['returnCode']   = '200';
                $this->vdata['returnInfo'] = '获取成功';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= $info;
            }
        }
        $this->_send_json($this->vdata);
    }

    /**
     * 转发圈子
     */
    public function repeat_circle(){
        if(ENVIRONMENT=='production'){
            $this->_auto();
        }
        //获取当前用户信息
        $user_info = $this->userinfo;
        if(!isset($user_info) && empty($user_info)){
            $this->vdata['returnCode']   = '401';
            $this->vdata['returnInfo'] = '请求失败';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else{
            $uid = $user_info['id'];
            $data = $this->input->post();
            if(!isset($data) && empty($data)){
                $this->vdata['returnCode']   = '250';
                $this->vdata['returnInfo'] = '缺少参数';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= array();
            }else{
                $r_content = $this->input->post('r_content');
                $r_content1 = $this->input->post('r_content1');
                if(!isset($content) || trim($content)==''){
                    $this->vdata['returnCode']   = '250';
                    $this->vdata['returnInfo'] = '请填写分享心得';
                    $this->vdata['secure']     = JSON_SECURE;
                    $this->vdata['content']= array();
                }
                $rid = $data['rid'];
                $circle_info = $this->mcircle->get_one(array('id'=>$rid));
                if($circle_info['rid']==0){
                    if($circle_info['c_type']==1){
                        $create_data = array(
                            'uid'=>$uid,
                            'rid'=>$rid,    //转发圈子的ID
                            't_id'=>$circle_info['id'], //记录原始圈子的ID
                            'r_content1'=>userTextEncode(strip_tags($r_content1)),
                            'r_content'=>userTextEncode(strip_tags(trim($r_content))),
                            'timeline'=>time()
                        );
                    }else{
                        $create_data = array(
                            'uid'=>$uid,
                            'rid'=>$rid,    //转发圈子的ID
                            't_id'=>$circle_info['t_id'], //记录原始圈子的ID
                            'c_type'=>2,
                            'r_content1'=>userTextEncode(strip_tags($r_content1)),
                            'r_content'=>userTextEncode(strip_tags(trim($r_content))),
                            'timeline'=>time()
                        );
                    }
                }else{
                    if($circle_info['c_type']==1){
                        $create_data = array(
                            'uid'=>$uid,
                            'rid'=>$rid,    //转发圈子的ID
                            't_id'=>$circle_info['t_id'],//记录原始圈子的ID
                            'r_content1'=>userTextEncode(strip_tags($r_content1)),
                            'r_content'=>userTextEncode(strip_tags(trim($r_content))),
                            'timeline'=>time()
                        );
                    }else{
                        $create_data = array(
                            'uid'=>$uid,
                            'rid'=>$rid,    //转发圈子的ID
                            't_id'=>$circle_info['t_id'],//记录原始圈子的ID
                            'c_type'=>2,
                            'r_content1'=>userTextEncode(strip_tags($r_content1)),
                            'r_content'=>userTextEncode(strip_tags(trim($r_content))),
                            'timeline'=>time()
                        );
                    }
                }

                $circle_id = $this->mcircle->create($create_data);
                if($circle_id){
                    //转发成功之后将上一条的转发圈子的转发数+1
                    $this->mcircle->set($rid,array('repeats','repeats + 1'));
                    $this->vdata['returnCode']   = '200';
                    $this->vdata['returnInfo'] = '转发成功';
                    $this->vdata['secure']     = JSON_SECURE;
                    $this->vdata['content']= $circle_id;
                }else{
                    $this->vdata['returnCode']   = '250';
                    $this->vdata['returnInfo'] = '转发失败';
                    $this->vdata['secure']     = JSON_SECURE;
                    $this->vdata['content']= array();
                }
            }
        }
        $this->_send_json($this->vdata);
    }

    /**
     * 圈子详情
     */
    public function circle_detail(){
        $circle_id = $this->input->post('id');
        $cirle_info = $this->mcircle->get_one(array('id'=>$circle_id,'is_deleted'=>0,'audit'=>1),'id,uid,c_type,t_id,rid,r_content,r_content1,content,locations,repeats,comments,click,photo,is_deleted,timeline');
        if(empty($cirle_info)){
            $this->vdata['returnCode']   = '250';
            $this->vdata['returnInfo'] = '圈子不存在或已删除';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else{
            if($cirle_info['uid']==$this->userinfo['id']){
                $info['is_can']=1;
            }else{
                $info['is_can']=0;
            }

            $info['content'] = str_replace('&nbsp;','',strip_tags(userTextDecode($cirle_info['content'])));
            $user_info = $this->maccount->get_one(array('id'=>$cirle_info['uid']),'id,uname,photo');
            $info['uname'] = $user_info['uname'];
            $info['uid'] = $user_info['id'];
            $photo = $this->mupload->get_one(array('id'=>$user_info['photo']),'url');
            if(!empty($photo)){
                $info['headimg'] = UPLOAD_URL.$photo['url'];
            }else{
                $info['headimg'] = '';
            }
            $info['id'] = $circle_id;
            $now_time = time();
            if($cirle_info['timeline']+24*3600 < $now_time){
                $info['cre_time'] = date('m-d H:i',$cirle_info['timeline']);
            }else{
                if($cirle_info['timeline']+3600 < $now_time){
                    $surpose_time = $now_time-$cirle_info['timeline'];
                    $res = round($surpose_time/3600);
                    $info['cre_time'] = $res.'小时前';
                }elseif($cirle_info['timeline']+60 < $now_time){
                    $surpose_time = $now_time-$cirle_info['timeline'];
                    $res = round($surpose_time/60);
                    $info['cre_time'] = $res.'分钟前';
                }elseif($now_time - $cirle_info['timeline'] < 60){
                    $info['cre_time'] = '1分钟内';
                }
            }
            $info['locations'] ='';
            if($cirle_info['locations']!=''){
                $info['locations'] = $cirle_info['locations'];
            }
            $info['circle_photo'] = array();
            if($cirle_info['photo']!=''){
                $c_photos = explode(',',$cirle_info['photo']);
                foreach($c_photos as $c_photo){
                    $img = $this->mupload->get_one(array('id'=>$c_photo),'url');
                    if(!empty($img)){
                        $info['circle_photo'][] = UPLOAD_URL.$img['url'];
                    }
                }
            }
            $info['repeats'] = $cirle_info['repeats'];
            $info['comments'] = $cirle_info['comments'];
            $info['rid'] = $cirle_info['rid'];
            if($cirle_info['rid']==0){
                $this->vdata['returnCode']   = '200';
                $this->vdata['returnInfo'] = '获取成功';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= $info;
            }else{
                if($cirle_info['c_type']==1){
                    //获取转发者名字和分享心得
                    $info['r_content'] = content_br(userTextDecode($cirle_info['r_content']));
                    //获取原圈子的内容
                    $start_info = $this->mcircle->get_one(array('id'=>$cirle_info['t_id'],'is_deleted'=>0,'audit'=>1),'id,uid,c_type,t_id,rid,r_content,r_content1,content,locations,repeats,comments,click,photo,is_deleted');
                    if(!empty($start_info)){
                        $uinfo = $this->maccount->get_one(array('id'=>$start_info['uid']),'id,uname,photo');
                        $start_info['uname'] = $uinfo['uname'];
                        $start_info['circle_photo'] = array();
                        if($start_info['photo']!=''){
                            $c_photos = explode(',',$start_info['photo']);
                            foreach($c_photos as $c_photo){
                                $img = $this->mupload->get_one(array('id'=>$c_photo),'url');
                                if(!empty($img)){
                                    $start_info['circle_photo'][] = UPLOAD_URL.$img['url'];
                                }else{
                                    $start_info['circle_photo'][] = '';
                                }
                            }
                        }
                        $start_info['c_type'] = 1;
                        if($start_info['content']!=''){
                            $string1 = str_replace('&nbsp;','',strip_tags(userTextDecode($start_info['content'])));
                            if(strlen($string1) > 100){
                                $start_info['content'] = content_br(mb_substr($string1,0,45,'utf-8')).'...';
                            }else{
                                $start_info['content'] = content_br($string1);
                            }
                        }else{
                            $start_info['content'] = '';
                        }
                    }else{
                        $start_info['content'] = '原圈子已删除';
                        $start_info['c_type'] = 1;
                        $start_info['deleted'] = 1;
                    }
                    $info['start'] = $start_info;
                    $this->vdata['returnCode']   = '200';
                    $this->vdata['returnInfo'] = '获取成功';
                    $this->vdata['secure']     = JSON_SECURE;
                    $this->vdata['content']= $info;
                }elseif($cirle_info['c_type']==2){
                    //获取转发者名字和分享心得
                    $info['r_content'] = content_br(userTextDecode($cirle_info['r_content']));
                    //获取原长文章的内容
                    $start_info = $this->mlarticle->get_one(array('id'=>$cirle_info['t_id'],'is_deleted'=>0,'audit'=>1),'id,uid,repeats,comments,collects,praise,zans,zan_money,title,content,click,photo,is_deleted,is_zan');
                    if(empty($start_info)){
                        $start_info['content'] = '原文章已删除';
                        $start_info['c_type'] = 2;
                        $start_info['deleted'] = 1;
                        $info['start'] = $start_info;
                    }else{
                        $start_info['c_type'] = 2;
                        $start_info['deleted'] = 0;
                        if($start_info['content']!=''){
                            $string = str_replace('&nbsp;','',strip_tags(userTextDecode($start_info['content'])));
                            if(strlen($string) > 100){
                                $start_info['content'] = mb_substr($string,0,45,'utf-8').'...';
                            }else{
                                $start_info['content'] = $string;
                            }
                        }else{
                            $start_info['content'] = '';
                        }
                        $info['start'] = $start_info;
                    }
                    $this->vdata['returnCode']   = '200';
                    $this->vdata['returnInfo'] = '获取成功';
                    $this->vdata['secure']     = JSON_SECURE;
                    $this->vdata['content']= $info;
                }
            }
        }
        $this->_send_json($this->vdata);
    }
    public function circle_detailh5(){
        $circle_id = $this->input->post('id');
        $cirle_info = $this->mcircle->get_one(array('id'=>$circle_id,'is_deleted'=>0,'audit'=>1),'id,uid,c_type,t_id,rid,r_content,r_content1,content,locations,repeats,comments,click,photo,is_deleted,timeline');
        if(empty($cirle_info)){
            $this->vdata['returnCode']   = '250';
            $this->vdata['returnInfo'] = '圈子不存在或已删除';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else{
            if($cirle_info['uid']==$this->userinfo['id']){
                $info['is_can']=1;
            }else{
                $info['is_can']=0;
            }

            $info['content'] = str_replace('&nbsp;','',strip_tags(userTextDecode($cirle_info['content'])));
            $user_info = $this->maccount->get_one(array('id'=>$cirle_info['uid']),'id,uname,photo');
            $info['uname'] = $user_info['uname'];
            $info['uid'] = $user_info['id'];
            $photo = $this->mupload->get_one(array('id'=>$user_info['photo']),'url');
            if(!empty($photo)){
                $info['headimg'] = UPLOAD_URL.$photo['url'];
            }else{
                $info['headimg'] = '';
            }
            $info['id'] = $circle_id;
            $now_time = time();
            if($cirle_info['timeline']+24*3600 < $now_time){
                $info['cre_time'] = date('m-d H:i',$cirle_info['timeline']);
            }else{
                if($cirle_info['timeline']+3600 < $now_time){
                    $surpose_time = $now_time-$cirle_info['timeline'];
                    $res = round($surpose_time/3600);
                    $info['cre_time'] = $res.'小时前';
                }elseif($cirle_info['timeline']+60 < $now_time){
                    $surpose_time = $now_time-$cirle_info['timeline'];
                    $res = round($surpose_time/60);
                    $info['cre_time'] = $res.'分钟前';
                }elseif($now_time - $cirle_info['timeline'] < 60){
                    $info['cre_time'] = '1分钟内';
                }
            }
            $info['locations'] ='';
            if($cirle_info['locations']!=''){
                $info['locations'] = $cirle_info['locations'];
            }
            $info['circle_photo'] = array();
            if($cirle_info['photo']!=''){
                $c_photos = explode(',',$cirle_info['photo']);
                foreach($c_photos as $c_photo){
                    $img = $this->mupload->get_one(array('id'=>$c_photo),'url');
                    if(!empty($img)){
                        $info['circle_photo'][] = UPLOAD_URL.$img['url'];
                    }
                }
            }
            $info['repeats'] = $cirle_info['repeats'];
            $info['comments'] = $cirle_info['comments'];
            $info['rid'] = $cirle_info['rid'];
            if($cirle_info['rid']==0){
                $this->vdata['returnCode']   = '200';
                $this->vdata['returnInfo'] = '获取成功';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= $info;
            }else{
                if($cirle_info['c_type']==1){
                    //获取转发者名字和分享心得
                    $info['r_content'] = content_br(userTextDecode($cirle_info['r_content']));
                    //获取原圈子的内容
                    $start_info = $this->mcircle->get_one(array('id'=>$cirle_info['t_id'],'is_deleted'=>0,'audit'=>1),'id,uid,c_type,t_id,rid,r_content,r_content1,content,locations,repeats,comments,click,photo,is_deleted');
                    if(!empty($start_info)){
                        $uinfo = $this->maccount->get_one(array('id'=>$start_info['uid']),'id,uname,photo');
                        $start_info['uname'] = $uinfo['uname'];
                        $start_info['circle_photo'] = array();
                        if($start_info['photo']!=''){
                            $c_photos = explode(',',$start_info['photo']);
                            foreach($c_photos as $c_photo){
                                $img = $this->mupload->get_one(array('id'=>$c_photo),'url');
                                if(!empty($img)){
                                    $start_info['circle_photo'][] = UPLOAD_URL.$img['url'];
                                }else{
                                    $start_info['circle_photo'][] = '';
                                }
                            }
                        }
                        $start_info['c_type'] = 1;
                        if($start_info['content']!=''){
                            $string1 = str_replace('&nbsp;','',strip_tags(userTextDecode($start_info['content'])));
                            if(strlen($string1) > 100){
                                $start_info['content'] = content_br(mb_substr($string1,0,45,'utf-8')).'...';
                            }else{
                                $start_info['content'] = content_br($string1);
                            }
                        }else{
                            $start_info['content'] = '';
                        }
                    }else{
                        $start_info['content'] = '原圈子已删除';
                        $start_info['c_type'] = 1;
                        $start_info['deleted'] = 1;
                    }
                    $info['start'] = $start_info;
                    $this->vdata['returnCode']   = '200';
                    $this->vdata['returnInfo'] = '获取成功';
                    $this->vdata['secure']     = JSON_SECURE;
                    $this->vdata['content']= $info;
                }elseif($cirle_info['c_type']==2){
                    //获取转发者名字和分享心得
                    $info['r_content'] = content_br(userTextDecode($cirle_info['r_content']));
                    //获取原长文章的内容
                    $start_info = $this->mlarticle->get_one(array('id'=>$cirle_info['t_id'],'is_deleted'=>0,'audit'=>1),'id,uid,repeats,comments,collects,praise,zans,zan_money,title,content,click,photo,is_deleted,is_zan');
                    if(empty($start_info)){
                        $start_info['content'] = '原文章已删除';
                        $start_info['c_type'] = 2;
                        $start_info['deleted'] = 1;
                        $info['start'] = $start_info;
                    }else{
                        $start_info['c_type'] = 2;
                        $start_info['deleted'] = 0;
                        if($start_info['content']!=''){
                            $string = str_replace('&nbsp;','',strip_tags(userTextDecode($start_info['content'])));
                            if(strlen($string) > 100){
                                $start_info['content'] = mb_substr($string,0,45,'utf-8').'...';
                            }else{
                                $start_info['content'] = $string;
                            }
                        }else{
                            $start_info['content'] = '';
                        }
                        $info['start'] = $start_info;
                    }
                    $this->vdata['returnCode']   = '200';
                    $this->vdata['returnInfo'] = '获取成功';
                    $this->vdata['secure']     = JSON_SECURE;
                    $this->vdata['content']= $info;
                }
            }
        }
        $this->_send_json($this->vdata);
    }
    /**
     * 评论详情列表展示(下拉加载)
     */
    public function circle_comments(){
        $id = $this->input->post('id');
        $type = $this->input->post('type');
        $start = $this->input->post('page');
        $limit = 5;
        $start = $limit*($start-1);
        $comments = $this->mcomments->get_list($limit,$start,array('created'=>'desc'),array('rid'=>$id,'type'=>$type,'pid'=>0,'audit'=>1),"*");
        //获取当前用户信息
        $user_info = $this->userinfo;
        $uid = $user_info['id'];
        if(empty($comments)){
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '获取成功';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else{
            foreach($comments as $kk=>$vv){
                if($vv['uid']==$uid){
                    $comments[$kk]['is_can']=0;
                }else{
                    $comments[$kk]['is_can']=1;
                }
                $user_info = $this->maccount->get_one(array('id'=>$vv['uid']));
                $comments[$kk]['uname']=$user_info['uname'];
                $photo = $this->mupload->get_one(array('id'=>$user_info['photo']));
                if(!empty($photo)){
                    $comments[$kk]['headimg'] = UPLOAD_URL.$photo['url'];
                }else{
                    $comments[$kk]['headimg'] = '';
                }
                $comments[$kk]['cre_time'] = date('m-d H:i',$vv['created']);
                $comments[$kk]['content'] = userTextDecode($vv['content']);
                $child = $this->mcomments->get_all(array('rid'=>$id,'type'=>$type,'pid'=>$vv['id'],'audit'=>1),'*',array('created'=>'asc'));
                foreach($child as $key=>$val){
                    $user_info = $this->maccount->get_one(array('id'=>$val['uid']));
                    $user_info2 = $this->maccount->get_one(array('id'=>$val['chat_id']));
                    $child[$key]['uname']=$user_info['uname'];
                    $child[$key]['content'] = userTextDecode($val['content']);
                    if(!empty($user_info2)){
                        $child[$key]['cname']=$user_info2['uname'];
                    }else{
                        $child[$key]['cname']='';
                    }
                    if($val['uid']==$uid){
                        $child[$key]['is_can']=0;
                    }else{
                        $child[$key]['is_can']=1;
                    }
                }
                $comments[$kk]['child']=$child;
                $comments[$kk]['childnums']=count($child);
            }
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '获取成功';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= $comments;
        }
        $this->_send_json($this->vdata);
    }

    /**
     * 删除圈子
     */
    public function delete_circle(){
        if(ENVIRONMENT=='production'){
            $this->_auto();
        }
        //获取当前用户信息
        $user_info = $this->userinfo;
        $uid = $user_info['id'];
        $rid = $this->input->post('rid');
        $cirle_info = $this->mcircle->get_one(array('id'=>$rid,'is_deleted'=>0,'audit'=>1));
        if(empty($cirle_info)){
            $this->vdata['returnCode']   = '250';
            $this->vdata['returnInfo'] = '圈子不存在';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else {
            if($cirle_info['uid']==$uid){
                $this->db->query("update boc_circle set is_deleted=1 where id=$rid and uid=$uid");
                if($cirle_info['c_type']==2 && $cirle_info['rid']==0){
                    $article_id = $cirle_info['t_id'];
                    $this->db->query("update boc_long_article set is_deleted=1 where id=$article_id and uid=$uid");
                }
                $this->vdata['returnCode']   = '200';
                $this->vdata['returnInfo'] = '删除成功';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= array();
            }else{
                $this->vdata['returnCode']   = '300';
                $this->vdata['returnInfo'] = '没有权限';
                $this->vdata['secure']     = JSON_SECURE;
                $this->vdata['content']= array();
            }
        }
        $this->_send_json($this->vdata);
    }
}
